﻿




Public Class ImageOutputCtl



    Private Sub ImageOutputCtl_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Dim m_manager As LayerManager

    Dim iMethod As Integer = 1

    Dim bReadyView As Boolean = False

    Public Sub InitCtrl(ByRef m As LayerManager)
        m_manager = m

        If m_manager Is Nothing Then
            Return
        End If

        Dim layerDic As New Dictionary(Of String, Layer)
        layerDic = m_manager.GetLayersCopy()
        For Each kv As KeyValuePair(Of String, Layer) In layerDic

            Dim myItem As New FListItem(kv.Value.LdescProperty, kv.Key)
            dpdLayers.Items.Add(myItem)

        Next

        dpdLayers.DisplayMember = "DisplayText"
        dpdLayers.ValueMember = "InnerValue"

        '当没有图层存在的时候
        If dpdLayers.Items.Count > 0 Then

            dpdLayers.SelectedIndex = 0

        End If



        '对参数化，进行初始赋值

        Dim ext As MapWinGIS.Extents = m_manager.GetExtens()
        nudLeft.Value = ext.xMin
        nudRight.Value = ext.xMax

        nudTop.Value = ext.yMax
        nudBottom.Value = ext.yMin



        iMethod = 1

        bReadyView = True

        SetPreview()




    End Sub

    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
        If RadioButton1.Checked Then
            UseDefautView(True)
        Else

            UseDefautView(False)

        End If

    End Sub

    Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
        If RadioButton2.Checked Then

            UseLayerView(True)

        End If

    End Sub

    Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
        If RadioButton3.Checked Then
            UseSDView(True)
        End If

    End Sub

    Private Sub mapwin_MouseDownEvent(ByVal sender As System.Object, ByVal e As AxMapWinGIS._DMapEvents_MouseDownEvent) Handles AxMap1.MouseDownEvent

    End Sub

    Private Sub UseDefautView(ByVal b As Boolean)

        If b Then
           



            nudLeft.Enabled = False
            nudRight.Enabled = False

            nudBottom.Enabled = False
            nudTop.Enabled = False

            nudWidth.Enabled = False

            nudMulti.Enabled = False

            iMethod = 1

            dpdLayers.Enabled = False



          

            SetPreview()



        End If

    
    End Sub

    Private Sub UseLayerView(ByVal b As Boolean)
        If b Then
           

            nudLeft.Enabled = False
            nudRight.Enabled = False

            nudBottom.Enabled = False
            nudTop.Enabled = False




            nudWidth.Enabled = True


            nudMulti.Enabled = True

            dpdLayers.Enabled = True
        
            iMethod = 2


            SetPreview()


        End If



    End Sub

    Private Sub UseSDView(ByVal b As Boolean)
        If b Then


            nudLeft.Enabled = True
            nudRight.Enabled = True

            nudBottom.Enabled = True
            nudTop.Enabled = True


            nudWidth.Enabled = True

            dpdLayers.Enabled = False

            nudMulti.Enabled = False


            iMethod = 3

            SetPreview()


        End If

    End Sub


    Private Sub SetPreview()

        If Not bReadyView Then
            Return
        End If

        AxMap1.RemoveAllLayers()


        Dim img As MapWinGIS.Image

        If m_manager Is Nothing Then
            Return
        End If

        Select Case iMethod
            Case 1
                img = m_manager.GetImageFromMap()
            Case 2

                If dpdLayers.SelectedItem Is Nothing Then

                    Return
                End If

                Dim id As String = CType(dpdLayers.SelectedItem, FListItem).InnerValue



                Dim mLayer As Integer = m_manager.getLayerHandle(id)

                If mLayer = -1 Then
                    Return
                End If

                img = m_manager.GetImageFromMap(mLayer, nudMulti.Value, nudWidth.Value)

            Case 3

                img = m_manager.GetImageFromMap(nudLeft.Value, nudRight.Value, nudTop.Value, nudBottom.Value, nudWidth.Value)

            Case Else

                Return

        End Select

        If img Is Nothing Then
            Return
        End If


        AxMap1.AddLayer(img, True)




    End Sub



    Private Sub btnCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopy.Click
        Select Case iMethod
            Case 1
                m_manager.CopyImageToClipboard()


            Case 2

                Dim id As String = CType(dpdLayers.SelectedItem, FListItem).InnerValue

                If id Is Nothing Then
                    Return
                End If

                Dim iLayer As Integer = m_manager.getLayerHandle(id)
                m_manager.CopyImageToClipboardByLayerExtent(iLayer, nudMulti.Value, nudWidth.Value)

            Case 3

                m_manager.CopyImageToClipboardByParam(nudLeft.Value, nudRight.Value, nudTop.Value, nudBottom.Value, nudWidth.Value)

            Case Else

                Return

        End Select




    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        '小阳改, 删除 * png, 把bmp移动到前面
        'SaveFileDialog1.Filter = "jpeg file(*.jpg)|*.jpg|png file(*.png)|*.png|bmp file(*.bmp)|*.bmp|gif file(*.gif)|*.gif| All Files(*.*)|*.*"
        SaveFileDialog1.Filter = "bmp file(*.bmp)|*.bmp|jpeg file(*.jpg)|*.jpg|gif file(*.gif)|*.gif| All Files(*.*)|*.*"
        If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then

            Dim fPath As String = SaveFileDialog1.FileName

            If m_manager Is Nothing Then
                Return
            End If

            Select Case iMethod
                Case 1
                    m_manager.SaveImageFile(fPath)

                Case 2

                    Dim id As String = CType(dpdLayers.SelectedItem, FListItem).InnerValue

                    If id Is Nothing Then
                        Return
                    End If

                    Dim iLayer As Integer = m_manager.getLayerHandle(id)


                    m_manager.SaveImageFileByLayerExtent(fPath, iLayer, nudMulti.Value, nudWidth.Value)

                Case 3

                    m_manager.SaveImageFileByParam(fPath, nudLeft.Value, nudRight.Value, nudTop.Value, nudBottom.Value, nudWidth.Value)

                Case Else

                    Return

            End Select

        Else


        End If

    End Sub


    Private Sub nudMulti_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nudMulti.ValueChanged
        SetPreview()
    End Sub


    Private Sub dpdLayers_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dpdLayers.SelectedIndexChanged
        SetPreview()
    End Sub

    Private Sub nudWidth_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nudWidth.ValueChanged
        SetPreview()
    End Sub

    Private Sub nudTop_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nudTop.ValueChanged
        SetPreview()
    End Sub

    Private Sub nudBottom_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nudBottom.ValueChanged
        SetPreview()
    End Sub

    Private Sub nudLeft_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nudLeft.ValueChanged
        SetPreview()
    End Sub

    Private Sub nudRight_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nudRight.ValueChanged
        SetPreview()
    End Sub
End Class
